//==================================================================
// 文件名: V_BH_WorkOrder.cs
// 作 者：代码自动生成
// 日 期：2015/3/24 21:01:54
// 描 述：
// 版 本：1.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// 1.00             
//==================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.Text;
using Csla;
using lite;
using Csla.Validation;
using Csla.Data;
using System.Data.SqlClient;
using ZX.DAL;
using OpenExpressApp.ORM;

namespace DCBH.Library
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    [Table(Name = "V_BH_WorkOrder")]
    [ZX.DBModule.Table("V_BH_WorkOrder", "ID")]
    public class V_BH_WorkOrder : ZX.DBModule.BaseEntity
    {
        #region Business Methods 业务方法
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<Guid> IDProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<Guid>("ID"));
        [DataObjectField(true, true)]
        [Column, PK]
        public Guid ID
        {
            get { return GetProperty(IDProperty); }
            set { SetProperty(IDProperty, value); }
        }

        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> SystemGenNoProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<string>("SystemGenNo"));
        [Column]
        public string SystemGenNo
        {
            get { return GetProperty(SystemGenNoProperty); }
            set { SetProperty(SystemGenNoProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> WorkOrderNoProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<string>("WorkOrderNo"));
        [Column]
        public string WorkOrderNo
        {
            get { return GetProperty(WorkOrderNoProperty); }
            set { SetProperty(WorkOrderNoProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> BuProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<string>("Bu"));
        [Column]
        public string Bu
        {
            get { return GetProperty(BuProperty); }
            set { SetProperty(BuProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> CompanyNameProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<string>("CompanyName"));
        [Column]
        public string CompanyName
        {
            get { return GetProperty(CompanyNameProperty); }
            set { SetProperty(CompanyNameProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> CompanyTypeProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<string>("CompanyType"));
        [Column]
        public string CompanyType
        {
            get { return GetProperty(CompanyTypeProperty); }
            set { SetProperty(CompanyTypeProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> CompanyFlagProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<string>("CompanyFlag"));
        [Column]
        public string CompanyFlag
        {
            get { return GetProperty(CompanyFlagProperty); }
            set { SetProperty(CompanyFlagProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<DateTime> PlanDateProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<DateTime>("PlanDate"));
        [Column]
        public DateTime PlanDate
        {
            get { return GetProperty(PlanDateProperty); }
            set { SetProperty(PlanDateProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<DateTime> ProductStartDateProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<DateTime>("ProductStartDate"));
        [Column]
        public DateTime ProductStartDate
        {
            get { return GetProperty(ProductStartDateProperty); }
            set { SetProperty(ProductStartDateProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<DateTime> ProductEndDateProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<DateTime>("ProductEndDate"));
        [Column]
        public DateTime ProductEndDate
        {
            get { return GetProperty(ProductEndDateProperty); }
            set { SetProperty(ProductEndDateProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> BOMNoProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<string>("BOMNo"));
        [Column]
        public string BOMNo
        {
            get { return GetProperty(BOMNoProperty); }
            set { SetProperty(BOMNoProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> ItemNoProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<string>("ItemNo"));
        [Column]
        public string ItemNo
        {
            get { return GetProperty(ItemNoProperty); }
            set { SetProperty(ItemNoProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> ItemNameProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<string>("ItemName"));
        [Column]
        public string ItemName
        {
            get { return GetProperty(ItemNameProperty); }
            set { SetProperty(ItemNameProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> ItemModelProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<string>("ItemModel"));
        [Column]
        public string ItemModel
        {
            get { return GetProperty(ItemModelProperty); }
            set { SetProperty(ItemModelProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> EnglishNameProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<string>("EnglishName"));
        [Column]
        public string EnglishName
        {
            get { return GetProperty(EnglishNameProperty); }
            set { SetProperty(EnglishNameProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> ItemTypeProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<string>("ItemType"));
        [Column]
        public string ItemType
        {
            get { return GetProperty(ItemTypeProperty); }
            set { SetProperty(ItemTypeProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> ItemTypeTextProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<string>("ItemTypeText"));
        [Column]
        public string ItemTypeText
        {
            get { return GetProperty(ItemTypeTextProperty); }
            set { SetProperty(ItemTypeTextProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> ItemAttrProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<string>("ItemAttr"));
        [Column]
        public string ItemAttr
        {
            get { return GetProperty(ItemAttrProperty); }
            set { SetProperty(ItemAttrProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> ItemAttrTextProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<string>("ItemAttrText"));
        [Column]
        public string ItemAttrText
        {
            get { return GetProperty(ItemAttrTextProperty); }
            set { SetProperty(ItemAttrTextProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> WorkCenterProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<string>("WorkCenter"));
        [Column]
        public string WorkCenter
        {
            get { return GetProperty(WorkCenterProperty); }
            set { SetProperty(WorkCenterProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<decimal> WeightProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<decimal>("Weight"));
        [Column]
        public decimal Weight
        {
            get { return GetProperty(WeightProperty); }
            set { SetProperty(WeightProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<decimal> PriceProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<decimal>("Price"));
        [Column]
        public decimal Price
        {
            get { return GetProperty(PriceProperty); }
            set { SetProperty(PriceProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<decimal> AmountProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<decimal>("Amount"));
        [Column]
        public decimal Amount
        {
            get { return GetProperty(AmountProperty); }
            set { SetProperty(AmountProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<decimal> ReturnWeightProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<decimal>("ReturnWeight"));
        [Column]
        public decimal ReturnWeight
        {
            get { return GetProperty(ReturnWeightProperty); }
            set { SetProperty(ReturnWeightProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<decimal> WOBalProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<decimal>("WOBal"));
        [Column]
        public decimal WOBal
        {
            get { return GetProperty(WOBalProperty); }
            set { SetProperty(WOBalProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<decimal> ReturnQtyProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<decimal>("ReturnQty"));
        [Column]
        public decimal ReturnQty
        {
            get { return GetProperty(ReturnQtyProperty); }
            set { SetProperty(ReturnQtyProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<decimal> WOBalQtyProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<decimal>("WOBalQty"));
        [Column]
        public decimal WOBalQty
        {
            get { return GetProperty(WOBalQtyProperty); }
            set { SetProperty(WOBalQtyProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<decimal> OutsWeightProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<decimal>("OutsWeight"));
        [Column]
        public decimal OutsWeight
        {
            get { return GetProperty(OutsWeightProperty); }
            set { SetProperty(OutsWeightProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<decimal> PayAmountProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<decimal>("PayAmount"));
        [Column]
        public decimal PayAmount
        {
            get { return GetProperty(PayAmountProperty); }
            set { SetProperty(PayAmountProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<decimal> QtyProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<decimal>("Qty"));
        [Column]
        public decimal Qty
        {
            get { return GetProperty(QtyProperty); }
            set { SetProperty(QtyProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> RemarkProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<string>("Remark"));
        [Column]
        public string Remark
        {
            get { return GetProperty(RemarkProperty); }
            set { SetProperty(RemarkProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> ProcessingFlagProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<string>("ProcessingFlag"));
        [Column]
        public string ProcessingFlag
        {
            get { return GetProperty(ProcessingFlagProperty); }
            set { SetProperty(ProcessingFlagProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> ProcessingFlagTextProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<string>("ProcessingFlagText"));
        [Column]
        public string ProcessingFlagText
        {
            get { return GetProperty(ProcessingFlagTextProperty); }
            set { SetProperty(ProcessingFlagTextProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<Guid> AddUserProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<Guid>("AddUser"));
        [Column]
        public Guid AddUser
        {
            get { return GetProperty(AddUserProperty); }
            set { SetProperty(AddUserProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> AddUserNameProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<string>("AddUserName"));
        [Column]
        public string AddUserName
        {
            get { return GetProperty(AddUserNameProperty); }
            set { SetProperty(AddUserNameProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<DateTime> AddTimeProperty = RegisterProperty(typeof(V_BH_WorkOrder), new PropertyInfo<DateTime>("AddTime"));
        [Column]
        public DateTime AddTime
        {
            get { return GetProperty(AddTimeProperty); }
            set { SetProperty(AddTimeProperty, value); }
        }
        #endregion

        #region Validation Rules 验证规则
        //将验证规则与业务对象的属性联系在一起
        protected override void AddBusinessRules()
        {
            //AddRule的第一个参数是委托变量，也就是说是方法的变量
            ValidationRules.AddRule(CommonRules.StringRequired, "SystemGenNo");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("SystemGenNo", 30));
            ValidationRules.AddRule(CommonRules.StringRequired, "WorkOrderNo");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("WorkOrderNo", 30));
            ValidationRules.AddRule(CommonRules.StringRequired, "Bu");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Bu", 10));
            ValidationRules.AddRule(CommonRules.StringRequired, "CompanyName");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("CompanyName", 200));
            ValidationRules.AddRule(CommonRules.StringRequired, "CompanyType");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("CompanyType", 10));
            ValidationRules.AddRule(CommonRules.StringRequired, "CompanyFlag");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("CompanyFlag", 10));
            ValidationRules.AddRule(CommonRules.StringRequired, "BOMNo");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("BOMNo", 30));
            ValidationRules.AddRule(CommonRules.StringRequired, "ItemNo");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("ItemNo", 8));
            ValidationRules.AddRule(CommonRules.StringRequired, "ItemName");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("ItemName", 120));
            ValidationRules.AddRule(CommonRules.StringRequired, "ItemModel");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("ItemModel", 120));
            ValidationRules.AddRule(CommonRules.StringRequired, "EnglishName");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("EnglishName", 120));
            ValidationRules.AddRule(CommonRules.StringRequired, "ItemType");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("ItemType", 10));
            ValidationRules.AddRule(CommonRules.StringRequired, "ItemTypeText");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("ItemTypeText", 100));
            ValidationRules.AddRule(CommonRules.StringRequired, "ItemAttr");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("ItemAttr", 10));
            ValidationRules.AddRule(CommonRules.StringRequired, "ItemAttrText");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("ItemAttrText", 100));
            ValidationRules.AddRule(CommonRules.StringRequired, "WorkCenter");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("WorkCenter", 20));
            ValidationRules.AddRule(CommonRules.StringRequired, "Remark");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Remark", 500));
            ValidationRules.AddRule(CommonRules.StringRequired, "ProcessingFlag");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("ProcessingFlag", 1));
            ValidationRules.AddRule(CommonRules.StringRequired, "ProcessingFlagText");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("ProcessingFlagText", 100));
            ValidationRules.AddRule(CommonRules.StringRequired, "AddUserName");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("AddUserName", 30));
            //base.AddBusinessRules();
        }
        #endregion

        #region Factory Methods 工厂方法

        public V_BH_WorkOrder() { }

        public static V_BH_WorkOrder New()
        {
            return DataPortal.Create<V_BH_WorkOrder>();
        }
        public static V_BH_WorkOrder Get(Guid ID)
        {
            return DataPortal.Fetch<V_BH_WorkOrder>(new SingleLazyCriteria<V_BH_WorkOrder, Guid>(ID, false));
        }
        public static void Delete(Guid ID)
        {
            DataPortal.Delete(ID);
        }

        #endregion

        #region Data Access 数据访问
        //由于DataPortal_create中没有对数据库的访问，所有标记了Runlocal特性，使得该方法只需在本地运行即可
        //如果代码中有访问数据库的方法，那么就不要用这个特性，使数据门户可以根据情况将此方法的运行选择在应用服务器还是本地
        [RunLocal()]
        protected override void DataPortal_Create()
        {
            //在此调用CheckRules对所有的属性进行验证规则的检查，这样检查只运行一次。
            ValidationRules.CheckRules();
        }


        //可以看到此方法没有标记任何特性
        //此方法要访问数据库所以不能标记本地，由于她没有任何数据库更新操作所以也无需事务性的保护
        //此方法调用结束后会隐含的调用MarkOld（）方法
        private void DataPortal_Fetch(Guid ID)
        {
            //通过连接字符串建立SQL连接对象，Using符号代表，范围释放的意思，SqlConnection对象会在此范围后被释放掉
            using (var ctx = ConnectionManager<SqlConnection>.GetManager(ConnectionStringNames))
            {
                IDb db = DbFactory.Instance.GetDb(ctx.Connection);
                IQuery q = db.Query();
                q.Constrain("ID").Equal(ID);

                var data = db.Select<V_BH_WorkOrder>(q)[0];
                LoadProperty(IDProperty, data.ID);
                LoadProperty(SystemGenNoProperty, data.SystemGenNo);
                LoadProperty(WorkOrderNoProperty, data.WorkOrderNo);
                LoadProperty(BuProperty, data.Bu);
                LoadProperty(CompanyNameProperty, data.CompanyName);
                LoadProperty(CompanyTypeProperty, data.CompanyType);
                LoadProperty(CompanyFlagProperty, data.CompanyFlag);
                LoadProperty(PlanDateProperty, data.PlanDate);
                LoadProperty(ProductStartDateProperty, data.ProductStartDate);
                LoadProperty(ProductEndDateProperty, data.ProductEndDate);
                LoadProperty(BOMNoProperty, data.BOMNo);
                LoadProperty(ItemNoProperty, data.ItemNo);
                LoadProperty(ItemNameProperty, data.ItemName);
                LoadProperty(ItemModelProperty, data.ItemModel);
                LoadProperty(EnglishNameProperty, data.EnglishName);
                LoadProperty(ItemTypeProperty, data.ItemType);
                LoadProperty(ItemTypeTextProperty, data.ItemTypeText);
                LoadProperty(ItemAttrProperty, data.ItemAttr);
                LoadProperty(ItemAttrTextProperty, data.ItemAttrText);
                LoadProperty(WorkCenterProperty, data.WorkCenter);
                LoadProperty(WeightProperty, data.Weight);
                LoadProperty(PriceProperty, data.Price);
                LoadProperty(AmountProperty, data.Amount);
                LoadProperty(ReturnWeightProperty, data.ReturnWeight);
                LoadProperty(WOBalProperty, data.WOBal);
                LoadProperty(ReturnQtyProperty, data.ReturnQty);
                LoadProperty(WOBalQtyProperty, data.WOBalQty);
                LoadProperty(OutsWeightProperty, data.OutsWeight);
                LoadProperty(PayAmountProperty, data.PayAmount);
                LoadProperty(QtyProperty, data.Qty);
                LoadProperty(RemarkProperty, data.Remark);
                LoadProperty(ProcessingFlagProperty, data.ProcessingFlag);
                LoadProperty(ProcessingFlagTextProperty, data.ProcessingFlagText);
                LoadProperty(AddUserProperty, data.AddUser);
                LoadProperty(AddUserNameProperty, data.AddUserName);
                LoadProperty(AddTimeProperty, data.AddTime);
            }
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            //依然是调用下面的方法删除的，只是他此时可以获得自己的Id
            DataPortal_Delete(IDProperty);
        }
        #endregion
    }
}

